import 'dart:io';

import 'package:dio/dio.dart';
import 'package:wu_you/data/api/api.dart';
import 'package:wu_you/http/http.dart';
import 'package:wu_you/model/user/trad_model.dart';
import 'package:http_parser/http_parser.dart' as parser;

class TradRepository{
  factory TradRepository() => _getInstance();
  static TradRepository _instance;

  TradRepository._internal();

  static TradRepository _getInstance() {
    if (_instance == null) {
      _instance = TradRepository._internal();
    }
    return _instance;
  }

  ///交易区 产品列表
  Future<List<TradTypeModel>> getTradTypes() async {
    List<dynamic> mapResult = await Http().get(Api.tradTypeList);
    List<TradTypeModel> tt = mapResult.map((e) => TradTypeModel.fromJson(e)).toList();
    return tt;
  }

  ///交易发布信息
  Future<TradPubInfo> getPublishInfo(int wid) async {
    dynamic mapResult = await Http().get(Api.tradPushInfo, params: {"wid": wid});
    return Future.value(TradPubInfo.fromJson(mapResult));
  }

  ///发布广告
  Future<dynamic> publishAd(String wid,int type,String quantity,String price,String payment) async {
    Map<String, dynamic> data = {
      "wid":wid,
      "type":type.toString(),
      "quantity":quantity,
      "price":price,
      "payment":payment,
    };
    dynamic mapResult = await Http().post(Api.tradPublish,data: data);
    return mapResult;
  }

  ///收款方式查询
  Future<PaymentInfo> getPaymentInfo(int payment) async {
    dynamic mapResult = await Http().get(Api.paymentInfo, params: {"payment": payment});
    if(mapResult != null){
      return Future.value(PaymentInfo.fromJson(mapResult));
    }
    return null;
  }

  ///获取所有收款方式
  Future<List<PayMethodStatus>> getPaymentAll() async {
    List<dynamic> mapResult = await Http().get(Api.paymentAll);
    List<PayMethodStatus> pm = mapResult.map((e) => PayMethodStatus.fromJson(e)).toList();
    return pm;
  }

  ///绑定收款方式
  Future<dynamic> bindPayment({int payment,String bankName,String bankRealName,String bankAccount,String alipayImg,
  String tngImg,String wechatImg,String usdtWallet,String code,String mobile,String name,String idcard}) async {
    Map<String, dynamic> data = {
      "payment":payment,
      "mobile":mobile,
      "bankRealName":name,
    };
    if(payment == 1){ ///银行卡
      Map<String,dynamic> bank = {
        "bankName": bankName,
        "bankRealName":bankRealName,
        "bankAccount":bankAccount,
        "code":code,
        "idcard":idcard
      };
      data.addAll(bank);
    }else if(payment == 2){
      Map<String,dynamic> alipay = {
        "alipayImg": alipayImg,
        "code":code,
      };
      data.addAll(alipay);
    }else if(payment == 3){
      Map<String,dynamic> wx = {
        "wechatImg": wechatImg,
        "code":code,
      };
      data.addAll(wx);
    }else if(payment == 4){
      Map<String,dynamic> usdt = {
        "usdtWallet": usdtWallet,
        "code":code,
      };
      data.addAll(usdt);
    }else if(payment == 5){
      Map<String,dynamic> tng = {
        "tngImg": tngImg,
        "code":code,
      };
      data.addAll(tng);
    }

    dynamic mapResult = await Http().post(Api.bindPayment,data: data);
    return mapResult;
  }

  ///上传图片 1.收款二维码2:头像3:申诉
  Future<dynamic>uploadImage(File file,int type)async{
    File newFile = file;
    FormData formData =  FormData.fromMap({
      "type":type,
      "file": await MultipartFile.fromFile(
        newFile.path, //图片路径
        contentType: parser.MediaType.parse("image/jpeg"),
        // contentType: parser.MediaType.parse("multipart/form-data"),
      ),});
    dynamic result = await Http().post(Api.uploadFile, data: formData);
    // FileSuccessModel model = FileSuccessModel.fromJson(result);
    return result;
  }

  ///产品订单列表
  Future<TradOrders> getTradOrders(String wid,int size) async {
    Map<String,dynamic> data = {
      "wid": wid,
      "size":size,
    };
    dynamic mapResult = await Http().get(Api.tradSingleOrders,params: data);
    if(mapResult != null){
      return Future.value(TradOrders.fromJson(mapResult));
    }
    return null;
  }

  ///产品订单展开列表
  Future<List<TradOrderExp>> getTradExpList(String size,String wid,String price,String type) async {
    Map<String,dynamic> data = {
      "size": size,
      "wid":wid,
      "price":price,
      "type":type
    };
    List<dynamic> mapResult = await Http().get(Api.tradExpList,params: data);
    List<TradOrderExp> list = [];
    if(mapResult != null){
       list = mapResult.map((e) => TradOrderExp.fromJson(e)).toList();
    }
    return list;
  }

  ///用户下单
  Future<dynamic> tradGoodAdd(String id,String type) async {
    Map<String, dynamic> data = {
      "id":id,
      "type":type,
    };
    dynamic mapResult = await Http().post(Api.tradGoodAdd,data: data,showExtraInfo: true);
    return mapResult;
  }

  ///我的广告列表
  Future<List<MyAdOrders>> getMyAdList(String status,int page,int limit) async {
    Map<String,dynamic> data = {
      "status": status,
      "page":page,
      "limit":limit,
    };
    List<dynamic> mapResult = await Http().get(Api.myAdList,params: data);
    List<MyAdOrders> list = [];
    if(mapResult != null){
      list = mapResult.map((e) => MyAdOrders.fromJson(e)).toList();
    }
    return list;
  }

  ///我的广告订单详情
  Future<TradOrderDetail> getAdDetailInfo(int id) async {
    Map<String,dynamic> data = {
      "id": id,
    };
    dynamic mapResult = await Http().get(Api.myAdItemInfo,params: data);
    if(mapResult != null){
      return Future.value(TradOrderDetail.fromJson(mapResult));
    }
    return null;
  }

  ///下架发布的广告
  Future<dynamic> cancelMyAd(String id) async {
    Map<String, dynamic> data = {
      "id":id,
    };
    dynamic mapResult = await Http().post(Api.cancelAd,data: data);
    return mapResult;
  }

  ///取消订单
  Future<dynamic> cancelAdOrder(String id) async {
    Map<String, dynamic> data = {
      "id":id,
    };
    dynamic mapResult = await Http().post(Api.cancelOrder,data: data);
    return mapResult;
  }

  ///确认支付
  Future<dynamic> orderPayment(String id,String certificate,String pay_type) async {
    Map<String, dynamic> data = {
      "id":id,
      "certificate":certificate,
      "pay_type":pay_type
    };
    dynamic mapResult = await Http().post(Api.orderPaySuccess,data: data);
    return mapResult;
  }

  ///驳回凭证
  Future<dynamic> orderTurnMark(String id,String mark) async {
    Map<String, dynamic> data = {
      "id":id,
      "mark":mark,
    };
    dynamic mapResult = await Http().post(Api.orderTurnMark,data: data);
    return mapResult;
  }

  ///订单放行确认交易
  Future<dynamic> orderRelease(String id,String pay_password) async {
    Map<String, dynamic> data = {
      "id":id,
      "pay_password":pay_password,
    };
    dynamic mapResult = await Http().post(Api.orderRelease,data: data);
    return mapResult;
  }

  ///订单申诉
  Future<dynamic> orderAppeal(String id,String mark) async {
    Map<String, dynamic> data = {
      "id":id,
      "mark":mark,
    };
    dynamic mapResult = await Http().post(Api.orderAppeal,data: data);
    return mapResult;
  }
}